package cn.com.sp.demo.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class AddGit {

	/**
	 * 复制文件夹里面的文件
	 *
	 * @author tcf
	 * @date 2018年12月18日 下午3:05:59
	 * @param sourcePath
	 * @param newPath
	 * @throws IOException
	 */
	public static void copyDir(String sourcePath, String newPath) throws IOException {
        File file = new File(sourcePath);
        if(!file.exists()){
        	System.out.println("原文件夹不存在");
        	return;
        }
        String[] filePath = file.list();
        File newFile = new File(newPath);
        if (newFile.exists()) {
        	newFile.delete();
        	newFile.mkdir();
           // (new File(newPath)).mkdir();
        }else{
        	newFile.mkdir();
        }
        
        for (int i = 0; i < filePath.length; i++) {
            if ((new File(sourcePath + file.separator + filePath[i])).isDirectory()) {
                copyDir(sourcePath  + file.separator  + filePath[i], newPath  + file.separator + filePath[i]);
            }
            
            if (new File(sourcePath  + file.separator + filePath[i]).isFile()) {
            	File fromFile = new File(sourcePath  + file.separator + filePath[i]);
            	File toFile = new File(newPath + file.separator + filePath[i]);
                copyFile(fromFile, toFile);
            }
            
        }
    }
	
	/**
	 * 复制文件
	 *
	 * @author tcf
	 * @date 2018年12月18日 下午2:22:44
	 * @param fromFile
	 * @param toFile
	 * @throws IOException
	 */
	public static void copyFile(File fromFile, File toFile) throws IOException {
		FileInputStream ins = new FileInputStream(fromFile);
		FileOutputStream out = new FileOutputStream(toFile);
		byte[] b = new byte[1024];
		int n = 0;
		while ((n = ins.read(b)) != -1) {
			out.write(b, 0, n);
		}
		ins.close();
		out.close();
	}
	
	/**
	 * Pom
	 *
	 * @author tcf
	 * @date 2018年12月18日 下午2:54:27
	 */
	private static void PomCopy() {
		System.out.println("pom更新开始=====");

		File fromFile = new File(sourcePom);
		if (!fromFile.exists()) {
			System.out.println(sourcePom + ",原文件不存在");
			return;
		}
			File toFile = new File(newPathPom);
			if (toFile.exists()) {
				toFile.delete();
			}
			try {
				copyFile(fromFile, toFile);
			} catch (IOException e) {
				System.out.println("复制文件出错");
				return;
			}
		System.out.println("pom更新完成=====");
	}
	
	/**
	 * src
	 *
	 * @author tcf
	 * @date 2019年1月8日 上午9:19:26
	 */
	private static void srcCopy() {
		System.out.println("src开始===");
		try {
			copyDir(sourceSrc,newPathSrc);
		} catch (IOException e) {
			System.out.println("src复制错误");
		}
		System.out.println("src结束===");
	}
	
	private static final String sourceSrc = "E:\\fndwork\\spring-boot-demo\\src";
	private static final String newPathSrc = "F:\\workspace\\spring-boot-demo\\src";
	
	private static final String sourcePom = "E:\\fndwork\\spring-boot-demo\\pom.xml";
	private static final String newPathPom = "F:\\workspace\\spring-boot-demo\\pom.xml";
	
	

	public static void main(String[] args) {
		srcCopy();
		PomCopy();

	}

}
